package com.rongke.web.controller.doctor;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.Doctor;
import com.rongke.model.User;
import com.rongke.service.DoctorService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @DoctorController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/expert")
@Transactional
public class DoctorController {
	private static final Logger LOGGER = Logger
			.getLogger(DoctorController.class);

	@Autowired
	private DoctorService doctorService;

	/**
	 * @根据id查找
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectDoctor(Long id) {
		LOGGER.debug("selectDoctor");

		try {
			Doctor doctor = doctorService.selectById(id);
			return JsonResp.ok(doctor);
		} catch (Exception e) {
			LOGGER.error("selectDoctor failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @获取专家组列表
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public JsonResp selectList(int cancerType, Page page) {
		LOGGER.debug("selectList");

		if (0 == cancerType) {
			Page<Doctor> doctorList = doctorService.selectPage(page);
			return JsonResp.ok(doctorList);
		} else {
			EntityWrapper<Doctor> ew = new EntityWrapper<>();
			ew.eq("cancer_type", cancerType);
			Page<Doctor> doctorList = doctorService.selectPage(page, ew);
			return JsonResp.ok(doctorList);
		}
	}

	/**
	 * @获取专家组列表
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectLeaderList", method = RequestMethod.GET)
	public JsonResp selectLeaderList(int cancerType, Page page) {
		LOGGER.debug("selectList");

		if (0 == cancerType) {
			EntityWrapper<Doctor> ew = new EntityWrapper<>();
			ew.eq("leader", 1);
			Page<Doctor> doctorList = doctorService.selectPage(page);
			return JsonResp.ok(doctorList);
		} else {
			EntityWrapper<Doctor> ew = new EntityWrapper<>();
			ew.eq("cancer_type", cancerType);
			ew.eq("leader", 1);
			Page<Doctor> doctorList = doctorService.selectPage(page, ew);
			return JsonResp.ok(doctorList);
		}
	}
}
